package codingforgreat.lchot150;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Lc341 {
       public interface NestedInteger {
  
               // @return true if this NestedInteger holds a single integer, rather than a nested list.
               public boolean isInteger();
  
               // @return the single integer that this NestedInteger holds, if it holds a single integer
               // Return null if this NestedInteger holds a nested list
               public Integer getInteger();
  
               // @return the nested list that this NestedInteger holds, if it holds a nested list
               // Return empty list if this NestedInteger holds a single integer
               public List<NestedInteger> getList();
   }
    public class NestedIterator implements Iterator<Integer> {
        List<Integer> help;
        int index;
        public NestedIterator(List<NestedInteger> nestedList) {
            help = new ArrayList<>();
            index = 0;
            add(nestedList);

        }
        private void add(List<NestedInteger> nestedList) {
            for (NestedInteger nestedInteger : nestedList) {
                if (nestedInteger.isInteger()) {
                    help.add(nestedInteger.getInteger());
                } else {
                    add(nestedInteger.getList());
                }
            }
        }

        @Override
        public Integer next() {
            return help.get(index++);

        }

        @Override
        public boolean hasNext() {
            return index < help.size();

        }
    }
}
